/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.accounting.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.javaeye.accounting.dao.MaterialsMonthBatchDetailDAO;
import com.javaeye.accounting.dto.MaterialsMonthBatchDetail;
import com.javaeye.common.util.ListUtil;
import com.javaeye.store.dao.MaterialsBatchDetailDAO;
import com.javaeye.store.dao.MaterialsDAO;
import com.javaeye.store.dao.MaterialsSKUDetailDAO;
import com.javaeye.store.dto.Materials;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.dto.MaterialsSKUDetail;

public class MaterialsMonthBatchDetailService implements IMaterialsMonthBatchDetailService {

	private MaterialsMonthBatchDetailDAO dao;
	
	private MaterialsSKUDetailDAO materialsSKUDetailDao;
	
	private MaterialsBatchDetailDAO materialsBatchDetailDao;
	
	private MaterialsDAO materialsDao;
	
	public MaterialsMonthBatchDetailService() {
		
	}
	
	public void setDao(MaterialsMonthBatchDetailDAO dao) {
		this.dao = dao;
	}

	public void setMaterialsBatchDetailDao(MaterialsBatchDetailDAO materialsBatchDetailDao) {
		this.materialsBatchDetailDao = materialsBatchDetailDao;
	}

	public void setMaterialsSKUDetailDao(MaterialsSKUDetailDAO materialsSKUDetailDao) {
		this.materialsSKUDetailDao = materialsSKUDetailDao;
	}

	public void setMaterialsDao(MaterialsDAO materialsDao) {
		this.materialsDao = materialsDao;
	}

	@Override
	public List<MaterialsMonthBatchDetail> getMaterialsBatchMonthInfo(
			String monthNo, int categoryId, String queryType) {
		List<MaterialsMonthBatchDetail> result = dao.getMonthInfo(monthNo, categoryId, queryType);
		return result;
	}
	
	/**
	 * 查询初始化库存明细,用于调整初始化库存，平时用不到
	 * @param monthNo
	 * @param materialsId
	 * @return
	 */
	public List<MaterialsMonthBatchDetail> getInitMaterialsBatchDetail(
			String monthNo, int materialsId) {
		List<MaterialsMonthBatchDetail> result = 
			new ArrayList<MaterialsMonthBatchDetail>();
		Materials materials = materialsDao.getMaterials(materialsId);
		if (materials != null) {
			Set<MaterialsSKUDetail> skuDetails = materials.getSkuDetails();
			for (MaterialsSKUDetail detail : skuDetails) {
				if (!detail.isInUse()) {
					continue;
				}
				int skuId = detail.getId();
				List<MaterialsMonthBatchDetail> batchInfo = 
					dao.getMonthInfo(monthNo, skuId);
				if (batchInfo == null || batchInfo.size() == 0) {
					MaterialsMonthBatchDetail newDetail = 
						new MaterialsMonthBatchDetail();
					newDetail.setMonthNo(monthNo);
					newDetail.setSkuDetail(detail);
					result.add(newDetail);
				} else {
					result.addAll(batchInfo);
				}
			}
		}
		return result;
	}
	
	/**
	 * 保存初始化库存信息，平时用不到
	 */
	public void saveInitMaterialsBatchDetail(String monthNo, List<Integer> detailIds, 
			List<Integer> skuDetailIds, List<MaterialsMonthBatchDetail> datas) {
		
		int oldSkuId = -1;
		for (int index = 0; index < detailIds.size(); index++) {
			MaterialsMonthBatchDetail data = datas.get(index);
			float number = data.getNumber();
			float defectiveNumber = data.getDefectiveNumber();
			String batchNo = data.getBatchNo();
			double price = data.getPrice();
			
			if (null == batchNo || "".equals(batchNo.trim())) {
				continue;
			}
			
			int skuId = skuDetailIds.get(index);
			MaterialsSKUDetail skuDetail = materialsSKUDetailDao.getMaterialsSKUDetail(skuId);
			int category = skuDetail.getMaterials().getRootCategory().getId();
			if (oldSkuId != skuId) {
				skuDetail.setNumber(number);
				skuDetail.setDefectiveNumber(defectiveNumber);
			} else {
				skuDetail.setNumber(skuDetail.getNumber() + number);
				skuDetail.setDefectiveNumber(skuDetail.getDefectiveNumber() + defectiveNumber);
			}
			materialsSKUDetailDao.saveMaterialsSKUDetail(skuDetail);
			
			//初始化期初库存数
			MaterialsMonthBatchDetail monthDetail = 
				dao.getMaterialsMonthBatchDetail(skuId, batchNo, monthNo);
			if (monthDetail == null) {
				monthDetail = new MaterialsMonthBatchDetail();
				monthDetail.setSkuDetail(skuDetail);
				monthDetail.setBatchNo(batchNo);
				monthDetail.setMonthNo(monthNo);
				monthDetail.setPrice(price);
			}
			monthDetail.setNumber(number);
			monthDetail.setDefectiveNumber(defectiveNumber);
			dao.saveMaterialsMonthBatchDetail(monthDetail);
			
			//更新批次库存数量
			MaterialsBatchDetail batchDetail = materialsBatchDetailDao.getBatchDetail(skuId, batchNo);
			if (batchDetail == null) {
				batchDetail = new MaterialsBatchDetail();
				batchDetail.setMaterialsId(skuId);
				batchDetail.setBatchNo(batchNo);
			}
			if (ListUtil.checkIsProcurementCategory(category)) {
				//合格品入库数量(原料)
				batchDetail.setInNumber(number);
				//不合格品入库数量(原料)
				batchDetail.setPlanNumber(defectiveNumber);
				batchDetail.setType(MaterialsBatchDetail.MATERIALS_BATCH_TYPE_MATERIALS);
			} else {
				//合格入库数量(成品)
				batchDetail.setNumber(number);
			}
			batchDetail.setPrice(price);
			materialsBatchDetailDao.save(batchDetail);
			
			oldSkuId = skuId;
		}
	}
}
